package com.yjf.ljr.activity;

import java.net.URLEncoder;
import java.util.regex.Pattern;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.yjf.ljr.R;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.connection.IjConnect;
import com.yjf.ljr.dataclass.SubmitNewPwdDataClass;
import com.yjf.ljr.parser.RequestBuilder;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.AESUtil;
import com.yjf.ljr.util.AppUtil;
import com.yjf.ljr.util.Base64;

public class SetPassWordActivity extends IjActivity implements OnClickListener {
	@ID("etNewPwd")
	private EditText mEtNewPwd;
	@ID("etRepeatNewPwd")
	private EditText mEtRepeatNewPwd;
	@ID("btComplete")
	private Button mBtComplete;
	private String code;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setpassword);
		initControl();
	}

	private void initControl() {
		setLeftBtnClick();
		setTitleStr("设置密码");
		mBtComplete.setOnClickListener(this);
		Intent intent = getIntent();

		code = intent.getStringExtra("obtain_code");
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.btComplete:
			String newPwd = mEtNewPwd.getText().toString().trim();
			String repeatPwd = mEtRepeatNewPwd.getText().toString().trim();
			if (TextUtils.isEmpty(newPwd)) {
				// showToast("新密码不能为空！");
				showToast("新密码不能为空！");
				return;
			}
			if (isChinese(newPwd)) {
				showToast("新密码不能包含中文");
				return;
			}

			if (newPwd.length() < 6 || newPwd.length() > 20) {
				showToast("新设置的密码长度应该在6-20个字符范围内");
				return;
			}

			if (AppUtil.isNumeric(newPwd)) {
				showToast("新密码不能全为数字");
				return;
			}

			if (AppUtil.isCharAllSame(newPwd)) {
				showToast("新密码不能使用完全相同的数字、字母或符号");
				return;
			}
			if (!AppUtil.hasDigit(newPwd)) {
				showToast("新密码不能全为字母");
				return;
			}

			if (TextUtils.isEmpty(repeatPwd)) {
				// showToast("重复密码不能为空！");
				showToast("重复密码不能为空！");
				return;
			}

			if (!(newPwd.equals(repeatPwd))) {
				// showToast("两个输入密码不一致");
				showToast("两个输入密码不一致");
				return;
			} else {
				if (newPwd.length() < 6 || newPwd.length() > 20)

				{
					// showToast("设置密码不在6-16个字符范围内");
					showToast("设置密码不在6-20个字符范围内");
					return;
				}
				showProgressDialog();

				new SubmitNewPassword(AESUtil.encode(newPwd), code).execute();
			}
			break;
		default:
			break;
		}
	}

	// 判断是不是中文
	public boolean isChinese(String name) {
		String str = "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$";

		boolean isIncludeChinese = false;
		for (int i = 0; i < name.length(); i++) {
			if (Pattern.matches(str, name.substring(i, i + 1))) {
				isIncludeChinese = true;
				break;
			}
		}
		return isIncludeChinese;
	}

	class SubmitNewPassword extends AsyncTask<Void, Void, Void> {

		private String mFailStr;
		private String newPassword;
		private String Code;
		SubmitNewPwdDataClass dc = new SubmitNewPwdDataClass();

		public SubmitNewPassword(String newPassword, String Code) {
			this.newPassword = newPassword;
			this.Code = Code;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			RequestObject requestObject = new RequestObject();
			requestObject.method = "forgetLoginPwdSub";
			requestObject.map.put("code", Code);
			String pwdStr = URLEncoder.encode(newPassword);
			requestObject.map.put("newPassword", pwdStr);

			String requestbuild = RequestBuilder.build(requestObject);
			try {

				IjConnect.doRequest(requestbuild, dc);
				if (dc.code == null) {
					mFailStr = getResources().getString(R.string.loaddata_exception);
				}
				if (!(CommonData.RESULT_SUCCESS.equals(dc.code))) {
					mFailStr = dc.msg;
				}
			} catch (Exception e) {
				mFailStr = getResources().getString(R.string.loaddata_exception);
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if ((TextUtils.isEmpty(mFailStr)) && "1".equals(dc.code)) {
				// showToast(dc.msg);
				showToast(dc.msg);
				dismissProgressDialog();
				finish();
			} else {
				showToast(dc.msg);
				dismissProgressDialog();
			}
		}

	}
}
